package com.tanguy.user.ui.trip.activity

import android.app.Activity
import android.content.Intent
import com.tanguy.user.Const
import com.tanguy.user.R
import com.tanguy.user.bean.orde.BaseOrderBean
import com.tanguy.user.bean.orde.StationOrderBean
import com.tanguy.user.bean.station.CarBean
import com.tanguy.user.bean.station.SiteBean
import com.tanguy.user.ui.map.activity.MapFindCoordinateActivity
import com.tanguy.user.ui.trip.activity.choice.SelectStationActivity
import com.tanguy.user.utlis.extend.click
import com.tanguy.user.utlis.extend.loge
import com.tanguy.user.utlis.extend.setTextRight
import org.jetbrains.anko.startActivityForResult

/**
 * Created by dab on 2017/10/24 0024 17:20
 * 接站的界面
 */
class ReceiveStationActivity : BasePickerActivity() {
    override fun getSpecialViewRes(): Int = R.layout.view_stub_receive_station
    override val type: Int = 3
    override fun getOrderBean(): BaseOrderBean =stationOrderBean
    private val stationOrderBean= StationOrderBean()
    var siteBean: SiteBean? = null
    override fun initView() {
        super.initView()
        click(R.id.ttv_start) {
            if (cityName != null) {
                startActivityForResult<SelectStationActivity>(Const.RequestCode.SELECT_STATION_START
                        , Const.IntentCode.CITY_NAME to cityName!!
                        , Const.IntentCode.CITY_CODE to cityCode!!
                )
            } else {
                showToast("定位没成功，请重试")
                location()
            }
        }
        click(R.id.ttv_end) {
            if (siteBean == null) {
                showToast("请选择站点")
                return@click
            }
            startActivityForResult<MapFindCoordinateActivity>(Const.RequestCode.TRIP_ADDRESS
                    , "cityCode" to siteBean!!.cityCode
                    , "cityName" to siteBean!!.cityName
                    , "type" to 2
                    , "lon" to siteBean!!.lon
                    , "lat" to siteBean!!.lat
            )

        }

    }
    override fun onCarTypeChanged(position: Int) {
        if (stationOrderBean.siteId.isNullOrEmpty())  return
        if (stationOrderBean.boulat.isNullOrEmpty() || stationOrderBean.boulon.isNullOrEmpty())  return
        calculatingPrice(stationOrderBean)
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == Const.RequestCode.SELECT_STATION_START && data != null) {
                val carBeanList = data.getParcelableArrayListExtra<CarBean>(Const.IntentCode.CAR_LIST)
                siteBean = data.getParcelableExtra(Const.IntentCode.SITE_BEAN)
                setTextRight(R.id.ttv_start, siteBean!!.name, R.color.color_000000)
                loge(siteBean.toString())
                if (carBeanList != null) {
                    carTypeAdapter.setNewData(carBeanList)
                }
                stationOrderBean.siteId=siteBean!!.id.toString()
                setTextRight(R.id.ttv_end,"你要去哪?", R.color.color_ababab)
                stationOrderBean.boulat=""
                stationOrderBean.boulon=""
                stationOrderBean.bourn=""
            }
            if (requestCode == Const.RequestCode.TRIP_ADDRESS && data != null) {
                val name = data.getStringExtra("name")
                val latitude = data.getStringExtra("latitude")
                val longitude = data.getStringExtra("longitude")
                val thisCityCode = data.getStringExtra("cityCode")
                if (!thisCityCode.isNullOrEmpty()&&thisCityCode != cityCode) {
                    loge("城市变了")
                    //接站不用请求,因为一出发点为准
//                    HttpManager.mapGetCar(thisCityCode)
//                            .requestSucceed(this) {
//                                dockingBean=it
//                                carTypeAdapter.setNewData(it.car)
//                            }
                } else {

                }
                setTextRight(R.id.ttv_end,name, R.color.color_000000)
                stationOrderBean.boulat=latitude
                stationOrderBean.boulon=longitude
                stationOrderBean.bourn=name
                calculatingPrice(stationOrderBean)

            }
        }
    }
}